<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <input type="text" v-model="val">
        <dom :value="val" @send="send"></dom>
    </div>
    <template id="first">
        <!-- vue2只能有一个根节点 -->
        <div id="content">
           <fieldset>
               <legend>我是子组件</legend>
               <button @click="add">btn</button>
                <ul>
                    <li v-for="(item,index) of arr" :key="index">
                        {{item}}
                    </li>
                </ul>

           </fieldset>
        </div>
    </template>
    <script src='https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js'></script>
    <script>
        //高复用
        //低耦合
        //易维护

        //全局创建组件
        //局部创建组件
        Vue.component("dom",{
            template:"#first",
            props:["value"],
            methods:{
                add(){
                    this.arr.unshift(this.value);
                    this.$emit("send",this.value);
                   
                }
            },
            //组件中data必须是函数
            //为了让每个组件都能维护一份返回对象的独立拷贝
            //避免组件互相污染
            data(){
                return{
                   arr:["玫瑰花的张力","带走我的回忆","让你多喝热水你不听"]
                }
            }
        })
        const vm = new Vue({
            el:"#app",
            data:{
                val:""
            },
            methods:{
                send(input){
                   console.log(input);
                   this.val="";
                }
            },
            watch:{},
            computed:{}
        })
    </script>
</body>
</html>